package com.example.dragon.xmf;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dragon.untis.TimeButton;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.SMSSDK;
import okhttp3.Call;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    EditText user_phone,user_code,user_pass;
    TimeButton timeButton;
    Button zhuce;
    String mob;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        user_phone = (EditText) findViewById(R.id.user_phone);
        user_code = (EditText) findViewById(R.id.user_code);
        user_pass = (EditText) findViewById(R.id.user_pass);
        timeButton = (TimeButton) findViewById(R.id.timeButton);
        zhuce = (Button) findViewById(R.id.zhuce);
        zhuce.setOnClickListener(this);
        timeButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.timeButton:
                mob = user_phone.getText().toString().trim();
//                if(Utils.isPhone(mob)){
                timeButton.startTime();
                SMSSDK.getVerificationCode("86", mob);
//                }else {
//                    showShortToast("手机号格式错误");
//                }
                break;
            case R.id.zhuce:

                registar();
                break;
            case R.id.reback:
                this.finish();
                break;
        }
    }


    private void registar(){
        OkHttpUtils.post()
                .url("http://xmf.api.0539cn.com/index.php?c=User&a=reg")
                .addParams("mob",user_phone.getText().toString())
                .addParams("pass",user_pass.getText().toString())
                .addParams("sms",user_code.getText().toString())
                .addParams("group","1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(jsonObject.getString("code").equals("200")){
                                Toast.makeText(RegisterActivity.this,jsonObject.getString("message"),Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(RegisterActivity.this,jsonObject.getString("message"),Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
